package aw;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.util.ArrayList;
import java.util.Map;

import utils.ObjectCombines;
import utils.ObjectMeaning;
import utils.ReadGameCfgXML;
import utils.ReadXMLFile;
import utils.World;

import common_interfaces.ServerInterface;

public class GameServer {
	static Map<Integer,ArrayList<String>> map;
	
	public static void main(String args[]){
		ReadXMLFile r = new ReadXMLFile();
		World world = r.createMap();
		
		
		/**
		 * Criei um xml onde os parametros do jogo sao definidos
		 * Neste momento apenas tem os objetos possiveis no mapa
		 * 
		 * E preciso verificar os objetos do mapa de acordo com o aStatic_Variables.acessories_available
		 * para os mesmos serem contruidos da maneira correta com o valor que corresponde
		 */
		System.out.println("Acessorios detetados: ");
		
		Object[] ze = ReadGameCfgXML.setupGameCfg();
		Static_Variables.objects_meaning = (ArrayList<ObjectMeaning>) ze[0];
		Static_Variables.objects_combines = (ArrayList<ObjectCombines>) ze[1];
		
		
		
		for(int i=0; i<Static_Variables.objects_meaning.size(); i++){
			ObjectMeaning novo = Static_Variables.objects_meaning.get(i);
			Static_Variables.objects_meaning_Hash.put(novo.get_id(), novo);
		}
		
		
		System.out.println("Fim Objectos detetados: ");
		
		// Prepara o jogo
		prepareGame();
		for(int i=0; i<world.getNumberofRooms(); i++){
			Static_Variables.posInits.add(i);
		}
		//Static_Variables.posInits.add(0);
		//Static_Variables.posInits.add(1);
		//Static_Variables.posInits.add(2);
		//Static_Variables.posInits.add(3);
		
		
		for(int total=world.getNumberofRooms(), i=0; i<total; i++ ){
			ArrayList<String> novo = new ArrayList<String>();
			
			for(int j=0; j<world.getRoom(i).getAcessories().size(); j++){
				System.out.println("Quarto: "+i+" com tamanho "+world.getRoom(i).getAcessories().size());
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("Objeto a colocar: "+world.getRoom(i).getAcessories().get(j).getId());
				if(world.getRoom(i).getAcessories().get(j).getId().equals("p")) {
					//novo.add(world.getRoom(i).getAcessories().get(j).toString());
				} else { novo.add(world.getRoom(i).getAcessories().get(j).toString()); }
			}
			Static_Variables.objectsByRoom.put(i, novo);
		}
			
		
		//Regista o jogo no RMI
		System.setSecurityManager(new RMISecurityManager());
        System.out.println("RMISecurityManager OK");
        try{
        	LocateRegistry.createRegistry(1099);
            ServerInterface server = new AlienWorldImpl();
            System.out.println("Interface Criada");
			Naming.rebind("AlienWorld", server); 
            System.out.println("Servidor AlienWorld criado");
        } catch(Exception e) {
        	System.out.println("Servidor AlienWorld main " + e.getMessage());
        }

        AlienWorldImpl.idGameReadytoStart();
    }
	
	public static void prepareGame() {
		/*
		Static_Variables.map = ReadMap.ReadMap("map.txt");
		
		// Printa o mapa para a consola
		for (int j = 0; j < Static_Variables.map.size(); j++) {
			ArrayList<String> line = Static_Variables.map.get(j);
			String line_string = "Linha: ";
			for (String string : line) {
				line_string = line_string + " " + string;
			}
			System.out.println(line_string);
		}*/
		
	}
}
